package com.yyf.mybatisdemo.controller;

import com.yyf.mybatisdemo.Service.UserService;
import com.yyf.mybatisdemo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("user/{id}")
    public User queryById(@PathVariable int id){
        return userService.queryById(id);
    }

    @GetMapping("/users")
    public List<User> getAllUsers(){
        return userService.getAllUsers();
    }

    @PostMapping("/user/add")
    public int insertUser(@RequestBody User user){ //因为落下注解@RequestBody，导致实体对象user的数据拿不到
        return userService.insertUser(user);
    }

    @PostMapping("/user/update")
    public void updateUser(@RequestBody User user){
        userService.updateUser(user);
    }

    @DeleteMapping("/user/{id}")
    public String deleteUser(@PathVariable int id){
        userService.deleteUser(id);
        return "删除成功";
    }

}
